package com.gexiaobao.pigeon.viewmodel

import androidx.lifecycle.MutableLiveData
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.model.bean.AccountSigningResponse
import com.gexiaobao.pigeon.app.model.bean.AccountUserInfo
import com.gexiaobao.pigeon.app.model.bean.ApplyBindBankCardResponse
import com.gexiaobao.pigeon.app.model.bean.BankLimitResponse
import com.gexiaobao.pigeon.app.model.bean.ChargeGebiInfoResponse
import com.gexiaobao.pigeon.app.model.bean.DepositApplyResponse
import com.gexiaobao.pigeon.app.model.bean.DepositApplySdkResponse
import com.gexiaobao.pigeon.app.model.bean.OrderDetailResponse
import com.gexiaobao.pigeon.app.model.bean.OrderListResponse
import com.gexiaobao.pigeon.app.model.bean.OrderTypeListResponse
import com.gexiaobao.pigeon.app.model.bean.OrgMemberTransferResultResponse
import com.gexiaobao.pigeon.app.model.bean.QueryBalanceResponse
import com.gexiaobao.pigeon.app.model.bean.QueryBankListResponse
import com.gexiaobao.pigeon.app.model.bean.ScanTransferResponse
import com.gexiaobao.pigeon.app.model.bean.SettingPaymentPwdResponse
import com.gexiaobao.pigeon.app.model.bean.UiState
import com.gexiaobao.pigeon.app.model.bean.WithDrawInfoResponse
import com.gexiaobao.pigeon.app.model.param.AccountBindPhoneBean
import com.gexiaobao.pigeon.app.model.param.AccountBindPhoneParam
import com.gexiaobao.pigeon.app.model.param.AccountRealNameParams
import com.gexiaobao.pigeon.app.model.param.ApplyBindCardParam
import com.gexiaobao.pigeon.app.model.param.DepositApplyParam
import com.gexiaobao.pigeon.app.model.param.DepositApplySDKParam
import com.gexiaobao.pigeon.app.model.param.IdCardParams
import com.gexiaobao.pigeon.app.model.param.MemberTransferParam
import com.gexiaobao.pigeon.app.model.param.PayParam
import com.gexiaobao.pigeon.app.model.param.SureBindCardParam
import com.gexiaobao.pigeon.app.model.param.UnBindBankBean
import com.gexiaobao.pigeon.app.network.ListDataUiState
import com.gexiaobao.pigeon.app.network.apiService
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.app.util.SettingUtil
import me.hgj.jetpackmvvm.base.appContext
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.callback.databind.StringObservableField
import me.hgj.jetpackmvvm.ext.request
import me.hgj.jetpackmvvm.state.ResultState
import okhttp3.RequestBody
import java.io.File

/**
 * @author: hxw
 * @date  : 2023/1/16
 * @desc  :
 */
class MineBankingViewModel : BaseViewModel() {

    private var pageNo = 1

    val allAmount = StringObservableField()
    val availableBalances = StringObservableField()
    val blockedBalances = StringObservableField()
    val gebiBalances = StringObservableField()

    val payeeName = StringObservableField()//收款人姓名
    val payeePhone = StringObservableField()//收款人电话
    val transferMoney = StringObservableField()//转账金额

    /** 会员转账-web组织 */
    val orgMemberTransferResult: MutableLiveData<OrgMemberTransferResultResponse> = MutableLiveData()
    fun orgMemberTransfer(body: RequestBody) {
        request({ apiService.orgMemberTransfer(body) }, {
            orgMemberTransferResult.value = it
        }, {
            RxToast.error(it.msg)
        })
    }

    /** 获取转账信息 */
    val getTransQrInfoResult: MutableLiveData<ScanTransferResponse> = MutableLiveData()
    fun getTransQrInfo(userId: Int, orgId: Int) {
        request({ apiService.getTransQrInfo(userId, orgId) }, {
            getTransQrInfoResult.value = it
        }, {
            RxToast.error(it.msg)
        })
    }

    /** 设置支付密码 */
    val setPaymentPwdResult = MutableLiveData<ResultState<SettingPaymentPwdResponse>>()
    fun setPayPwd() {
        request(
            { apiService.setPayPwd() },
            setPaymentPwdResult,
            false,
            appContext.getString(R.string.loading)
        )
    }

    /** 修改支付密码 */
    fun updatePayPwd() {
        request(
            { apiService.updatePayPwd() },
            setPaymentPwdResult,
            false,
            appContext.getString(R.string.loading)
        )
    }

    /** 重置支付密码 */
    fun resetPayPwd() {
        request(
            { apiService.resetPayPwd() },
            setPaymentPwdResult,
            false,
            appContext.getString(R.string.loading)
        )
    }

    /** 查询绑定的用户信息 */
    val accountUserInfoResult = MutableLiveData<ResultState<AccountUserInfo>>()
    fun getUserMemberInfo() {
        request(
            { apiService.getUserMemberInfo() },
            accountUserInfoResult,
            false,
            appContext.getString(R.string.loading)
        )
    }

    /** 账户提现协议签约 */
    val accountSignResult = MutableLiveData<ResultState<AccountSigningResponse>>()
    fun signAcctProtocol() {
        request(
            { apiService.signAcctProtocol() },
            accountSignResult,
            true,
            appContext.getString(R.string.loading)
        )
    }

    /** 查询余额 */
    val queryBalanceResult = MutableLiveData<ResultState<QueryBalanceResponse>>()
    fun queryBalance() {
        request(
            { apiService.queryBalance() },
            queryBalanceResult,
            false,
            appContext.getString(R.string.loading)
        )
    }

    /** 查询银行卡列表*/
    var queryBankListResult: MutableLiveData<ListDataUiState<QueryBankListResponse.ListBean>> = MutableLiveData()
    fun queryBankCard() {
        request(
            { apiService.queryBankCard() }, {
                val listDataUiState = ListDataUiState(
                    isSuccess = true,
                    isRefresh = true,
                    total = it.total,
                    isEmpty = it.isEmpty(),
                    isFirstEmpty = it.isEmpty(),
                    listData = it.list
                )
                queryBankListResult.value = listDataUiState
            }, {
                val listDataUiState =
                    ListDataUiState(
                        isSuccess = false,
                        isRefresh = false,
                        errMessage = it.msg,
                        listData = arrayListOf<QueryBankListResponse.ListBean>()
                    )
                queryBankListResult.value = listDataUiState
            }
        )
    }

    /** 提交实名认证 */
    val realNameResult: MutableLiveData<UiState> = MutableLiveData()
    fun setRealName(param: AccountRealNameParams) {
        val idCardFront = param.idCardFront
        val idCardBack = param.idCardBack
        val fileList = HashMap<String, File?>()
        val hashMap = HashMap<String, String>()
        hashMap["name"] = param.name
        hashMap["birthday"] = param.birthday
        hashMap["identityNo"] = param.identityNo
        hashMap["address"] = param.address
        hashMap["valid"] = param.valid

        if (idCardFront != null) {
            fileList["idCardFront"] = idCardFront
        } else {
            hashMap["idCardFront"] = ""
        }
        if (idCardBack != null) {
            fileList["idCardBack"] = idCardBack
        } else {
            hashMap["idCardBack"] = ""
        }
        val body = SettingUtil.filesToMultipartBody(fileList, hashMap)
        request({ apiService.setRealName(body) }, {
            val result = UiState(isSuccess = true)
            realNameResult.value = result
        }, {
            val result = UiState(isSuccess = false, errorMsg = it.msg)
            realNameResult.value = result
        })
    }

    /** 绑定手机 */
    val bindPhoneResult: MutableLiveData<UiState> = MutableLiveData()
    fun bindPhone(body: AccountBindPhoneBean) {
        request({ apiService.bindPhone(body) }, {
            val result = UiState(isSuccess = true)
            bindPhoneResult.value = result
        }, {
            val result = UiState(isSuccess = false, errorMsg = it.msg)
            bindPhoneResult.value = result
        })
    }

    /** 绑定手机 */
    val unBindBankResult: MutableLiveData<UiState> = MutableLiveData()
    fun unbindBankCard(body: UnBindBankBean) {
        request({ apiService.unbindBankCard(body) }, {
            val result = UiState(isSuccess = true)
            unBindBankResult.value = result
        }, {
            val result = UiState(isSuccess = false, errorMsg = it.msg)
            unBindBankResult.value = result
        })
    }

    /** 发送验证码 */
    val verCodeResult: MutableLiveData<UiState> = MutableLiveData()
    fun sendVerificationCode(body: AccountBindPhoneParam) {
        request({ apiService.sendVerificationCode(body) }, {
            val result = UiState(isSuccess = true)
            verCodeResult.value = result
        }, {
            val result = UiState(isSuccess = false, errorMsg = it.msg)
            verCodeResult.value = result
        })
    }

    /** 请求绑定银行卡 */
    val bindCardResult = MutableLiveData<ResultState<ApplyBindBankCardResponse>>()
    fun applyBindBankCard(body: ApplyBindCardParam) {
        request(
            { apiService.applyBindBankCard(body) },
            bindCardResult,
            false,
            appContext.getString(R.string.loading)
        )
    }

    /** 请求绑定银行卡 */
    fun applyBindBankCardV2(body: ApplyBindCardParam) {
        request(
            { apiService.applyBindBankCardV2(body) },
            bindCardResult,
            false,
            appContext.getString(R.string.loading)
        )
    }

    /** 充值申请SDK */
    val depositApplySDKResult = MutableLiveData<ResultState<DepositApplySdkResponse>>()
    fun depositApplySDK(body: DepositApplySDKParam) {
        request(
            { apiService.depositApplySDK(body) },
            depositApplySDKResult,
            false,
            appContext.getString(R.string.loading)
        )
    }

    /** 充值 */
    val depositApplyResult = MutableLiveData<ResultState<DepositApplyResponse>>()
    fun depositApply(body: DepositApplyParam) {
        request(
            { apiService.depositApply(body) },
            depositApplyResult,
            true,
            appContext.getString(R.string.loading)
        )
    }

    /** 提现 */
    val withdrawApplyResult = MutableLiveData<ResultState<DepositApplyResponse>>()
    fun withdrawApply(body: DepositApplyParam) {
        request(
            { apiService.withdrawApply(body) },
            withdrawApplyResult,
            false,
            appContext.getString(R.string.loading)
        )
    }

    /** 提现时鸽币抵扣说明 */
    val withdrawInfoResult: MutableLiveData<WithDrawInfoResponse> = MutableLiveData()
    fun getWithdrawInfo(money: Int) {
        request(
            { apiService.getWithdrawInfo(money) }, {
                withdrawInfoResult.value = it
            }, {
                RxToast.showToast(it.msg)
            })
    }

    /** 转账 */
    val memTransferResult = MutableLiveData<ResultState<DepositApplyResponse>>()
    fun memberTransfer(body: MemberTransferParam) {
        request(
            { apiService.memberTransfer(body) },
            memTransferResult,
            false,
            appContext.getString(R.string.loading)
        )
    }

    /** 确认支付 */
    val payResult = MutableLiveData<ResultState<DepositApplyResponse>>()
    fun commitPay(body: PayParam) {
        request(
            { apiService.commitPay(body) },
            payResult,
            false,
            appContext.getString(R.string.loading)
        )
    }

    /** 获取订单类型列表 */
    val orderTypeResult = MutableLiveData<ResultState<OrderTypeListResponse>>()
    fun getOrderTypeList() {
        request(
            { apiService.getOrderTypeList() },
            orderTypeResult,
            false,
            appContext.getString(R.string.loading)
        )
    }

    /** 获取订单详情  */
    val orderDetailResult = MutableLiveData<ResultState<OrderDetailResponse>>()
    fun getOrderDetail(mOrderId: Int) {
        request(
            { apiService.getOrderDetail(mOrderId) },
            orderDetailResult,
            false,
            appContext.getString(R.string.loading)
        )
    }

    /** 确认绑定银行卡 */
    val sureBindCardResult: MutableLiveData<UiState> = MutableLiveData()
    fun bindBankCard(body: SureBindCardParam) {
        request({ apiService.bindBankCard(body) }, {
            val result = UiState(isSuccess = true)
            sureBindCardResult.value = result
        }, {
            val result = UiState(isSuccess = false, errorMsg = it.msg)
            sureBindCardResult.value = result
        })
    }

    /** 确认绑定银行卡 */
    val unBindPhoneResult: MutableLiveData<UiState> = MutableLiveData()
    fun unbindPhone(body: AccountBindPhoneBean) {
        request({ apiService.unbindPhone(body) }, {
            val result = UiState(isSuccess = true)
            unBindPhoneResult.value = result
        }, {
            val result = UiState(isSuccess = false, errorMsg = it.msg)
            unBindPhoneResult.value = result
        })
    }

    /** 充值获取鸽币信息 */
    val gebiInfoResult: MutableLiveData<ChargeGebiInfoResponse> = MutableLiveData()
    fun getChargeGebiInfo(money: Int) {
        request({ apiService.getChargeGebiInfo(money) }, {
            gebiInfoResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 获取快捷产品支持银行列表及限额 */
    val bankLimitResult: MutableLiveData<BankLimitResponse> = MutableLiveData()
    fun getBankLimit() {
        request({ apiService.getBankLimit() }, {
            bankLimitResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 结息 */
    val tlementResult: MutableLiveData<Any> = MutableLiveData()
    fun settlement() {
        request({ apiService.settlement() }, {
            tlementResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 查询订单列表 */
    val orderListResult: MutableLiveData<ListDataUiState<OrderListResponse.ListBean>> = MutableLiveData()
    fun getOrderList(isRefresh: Boolean, pageSize: Int, bigTypeSelectPosition: Int, startTime: String, endTime: String) {
        if (isRefresh) {
            pageNo = 1
        }
        request({ apiService.getOrderList(startTime, endTime, bigTypeSelectPosition, pageNo, pageSize) }, {
            pageNo++
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                isEmpty = it.isEmpty(),
                total = it.total,
                hasMore = it.hasMore(),
                isFirstEmpty = isRefresh && it.isEmpty(),
                listData = it.list
            )
            orderListResult.value = listDataUiState
        }, {
            val listDataUiState =
                ListDataUiState(
                    isSuccess = false,
                    isRefresh = isRefresh,
                    errCode = it.code,
                    errMessage = it.msg,
                    listData = arrayListOf<OrderListResponse.ListBean>()
                )
            orderListResult.value = listDataUiState
        })
    }


    /** 上传清关身份证 */
    val uploadIdCardResult: MutableLiveData<UiState> = MutableLiveData()
    fun uploadIdCard(param: IdCardParams) {
        val idCardFront = param.idCardFront
        val idCardBack = param.idCardBack
        val fileList = HashMap<String, File?>()
        val hashMap = HashMap<String, String>()
        hashMap["name"] = param.name
        hashMap["birthday"] = param.birthday
        hashMap["code"] = param.code
        hashMap["address"] = param.address
        hashMap["valid"] = param.valid

        if (idCardFront != null) {
            fileList["idCardFront"] = idCardFront
        } else {
            hashMap["idCardFront"] = ""
        }
        if (idCardBack != null) {
            fileList["idCardBack"] = idCardBack
        } else {
            hashMap["idCardBack"] = ""
        }

        val body = SettingUtil.filesToMultipartBody(fileList, hashMap)
        request({ apiService.uploadIdCard(body) }, {
            val createResult = UiState(isSuccess = true)
            uploadIdCardResult.value = createResult
        }, {
            val createResult = it.errorLog?.let { it1 -> UiState(isSuccess = false, errorMsg = it1) }
            uploadIdCardResult.value = createResult
        })
    }

}